﻿/*=======================================================================================
	TASK 4:                                                     {Multidimentional Arrays}
	Write a program, that reads from the console an array of N integers and an integer K,
    sorts the array and using the method Array.BinSearch() finds the largest number
    in the array which is ≤ K. 
========================================================================================*/

using System;

class UseArray_BinarySearch
{
    static void Main()
    {
        int[] arr = { 1, 2, 4, -5, 6 };

        foreach (var e in arr)
        {
            Console.Write(e + " ");
        }
        Console.WriteLine();

        Array.Sort(arr);

        foreach (var e in arr)
        {
            Console.Write(e + " ");
        }
        Console.WriteLine();

        int target = -4;
        int index = Array.BinarySearch(arr, target);

        if (arr[0] > target)
        {
            Console.WriteLine("There isn't such a number in the array.");
        }
        else
        {
            if (index >= 0)
            {
                Console.WriteLine("{0}:this element is largest which is <=K", arr[index]);
            }
            
            else
            {
                Console.WriteLine("There isn't such a number in the array.");
                //Console.WriteLine(arr[-index - 2]);
            }
        }

    }
}

